cmake_minimum_required(VERSION 3.16)
project(frpc_server C CXX)
set(PROJECT_NAME frpc_server )

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++20")
#set(ENV{CODE_DIR} "/home/y/code")
set(project_root $ENV{CODE_DIR}/frpc_brpc)

#[[
execute_process(
    COMMAND bash -c "find ${PROJECT_SOURCE_DIR}/../.. -type d -regex \".*output/include$\" | head -n1 | xargs dirname | tr -d '\n'"
    OUTPUT_VARIABLE OUTPUT_PATH
)
]]
#set(CMAKE_PREFIX_PATH $ENV{CODE_DIR}/incubator-brpc/build/output/lib)

#config protobuf
include(FindThreads)
#include(FindProtobuf)
find_package(Protobuf REQUIRED)


include_directories(${PROTOBUF_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

protobuf_generate_cpp(PROTO_SRC PROTO_HEADER frpc.proto)
set(PROTO_SRC ${CMAKE_CURRENT_SOURCE_DIR}/frpc.pb.cc)
set(PROTO_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/frpc.pb.h)
#SET_SOURCE_FILES_PROPERTIES(${PROTO_SRC} ${PROTO_HEADER} PROPERTIES GENERATED TRUE)
#[[
add_custom_command(
          OUTPUT "${PROJECT_BINARY_DIR}/frpc.pb.cc"
                 "${PROJECT_BINARY_DIR}/frpc.pb.h"
          COMMAND  ${PROTOBUF_PROTOC_EXECUTABLE}
          ARGS --cpp_out  ${PROJECT_BINARY_DIR}
            -I ${CMAKE_SOURCE_DIR}
            frpc.proto
          DEPENDS ${CMAKE_SOURCE_DIR}/frpc.proto
          COMMENT "Running C++ protocol buffer compiler on "
          VERBATIM
        )

        ]]
# config brpc
#find_path(BRPC_INCLUDE_PATH NAMES brpc/server.h)
set(BRPC_INCLUDE_PATH $ENV{CODE_DIR}/incubator-brpc/build/output/include)
if(LINK_SO)
    #find_library(BRPC_LIB NAMES brpc)
    set(BRPC_LIB $ENV{CODE_DIR}/incubator-brpc/build/output/lib/libbrpc.so)
else()
    #find_library(BRPC_LIB NAMES libbrpc.a brpc)
     set(BRPC_LIB $ENV{CODE_DIR}/incubator-brpc/build/output/lib/libbrpc.a)
endif()
if((NOT BRPC_INCLUDE_PATH) OR (NOT BRPC_LIB))
    message(FATAL_ERROR "Fail to find brpc")
endif()
include_directories(${BRPC_INCLUDE_PATH})


###
find_path(GFLAGS_INCLUDE_PATH gflags/gflags.h)
find_library(GFLAGS_LIBRARY NAMES gflags libgflags)
if((NOT GFLAGS_INCLUDE_PATH) OR (NOT GFLAGS_LIBRARY))
    message(FATAL_ERROR "Fail to find gflags")
endif()
include_directories(${GFLAGS_INCLUDE_PATH})

execute_process(
    COMMAND bash -c "grep \"namespace [_A-Za-z0-9]\\+ {\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $2}' | tr -d '\n'"
    OUTPUT_VARIABLE GFLAGS_NS
)
if(${GFLAGS_NS} STREQUAL "GFLAGS_NAMESPACE")
    execute_process(
        COMMAND bash -c "grep \"#define GFLAGS_NAMESPACE [_A-Za-z0-9]\\+\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $3}' | tr -d '\n'"
        OUTPUT_VARIABLE GFLAGS_NS
    )
endif()

set(CMAKE_CPP_FLAGS "${DEFINE_CLOCK_GETTIME} -DGFLAGS_NS=${GFLAGS_NS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -DNDEBUG -O2 -D__const__= -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer")


find_path(LEVELDB_INCLUDE_PATH NAMES leveldb/db.h)
find_library(LEVELDB_LIB NAMES leveldb)
if ((NOT LEVELDB_INCLUDE_PATH) OR (NOT LEVELDB_LIB))
    message(FATAL_ERROR "Fail to find leveldb")
endif()
include_directories(${LEVELDB_INCLUDE_PATH})

if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
    set(OPENSSL_ROOT_DIR
        "/usr/local/opt/openssl"    # Homebrew installed OpenSSL
        )
endif()


find_package(OpenSSL)
include_directories(${OPENSSL_INCLUDE_DIR})

set(CMAKE_BUILD_TYPE "Debug")
include_directories(
${project_root}/src 
$ENV{CODE_DIR}/cppbase
$ENV{CODE_DIR}/filestore
)

set(CPPBASE_LIB $ENV{CODE_DIR}/cppbase/build/libcppbase.a)
set(Filestore_LIB $ENV{CODE_DIR}/filestore/build/libfilestore.a)
find_library(LIBRT rt)
find_package(workflow REQUIRED CONFIG HINTS $ENV{CODE_DIR}/workflow)
set(WORKFLOW_LIB workflow ${LIBRT})
link_directories(${WORKFLOW_LIB_DIR})


set(DYNAMIC_LIB
    ${CMAKE_THREAD_LIBS_INIT}
    ${GFLAGS_LIBRARY}
    ${PROTOBUF_LIBRARIES}
    ${LEVELDB_LIB}
    ${OPENSSL_CRYPTO_LIBRARY}
    ${OPENSSL_SSL_LIBRARY}
    ${CPPBASE_LIB}
    dl
    ) 
file(GLOB testserver_SRCS
     "./test_server/*.cpp"
)
file(GLOB testclient_SRCS
     "./test_client/*.cpp"
)
ADD_SUBDIRECTORY(src)
set(ALL_LIB 
${DYNAMIC_LIB}
${WORKFLOW_LIB}
frpc
handler
wf_task
${Filestore_LIB}
)
add_executable(t_server ${testserver_SRCS} ${PROTO_SRC} ${PROTO_HEADER})
add_executable(t_client ${testclient_SRCS} ${PROTO_SRC} ${PROTO_HEADER})

target_link_libraries(t_client ${BRPC_LIB} ${ALL_LIB})
target_link_libraries(t_server ${BRPC_LIB} ${ALL_LIB})







#[[
if(WIN32)
$ENV{CODE_DIR}/protobuf/install/include
elseif(UNIX)
/usr/local/include #protobuf include files
endif()
]]









